package org.kalai.unifypay.mins;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.kalai.unifypay.util.DateUtil;

public final class MinsPay {
	/**
	 * 民生银行支付商户入驻
	 * @param type	入驻类型	1-支付宝	2-微信
	 * @param tranxSN	订单号
	 * @param merchantId	商户号
	 * @param merchantName	商户名称
	 * @param shortName		商户简称
	 * @param merchantAddress 	商户地址
	 * @param servicePhone	客服电话
	 * @param category	经营类目
	 * @return
	 */
	public static Map<String, Object> merchantApply(int type, String tranxSN, String merchantId, String merchantName, String shortName, String merchantAddress, String servicePhone, String category){
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("payWay", type < 2 ? "ZFBZF" : "WXZF");
		paramMap.put("merchantId", merchantId+"_"+type);
		paramMap.put("merchantName", merchantName);
		paramMap.put("shortName", shortName);
		paramMap.put("merchantAddress", merchantAddress);
		paramMap.put("servicePhone", servicePhone);
		paramMap.put("category", category);
		String xmlMessage = MinsUtil.sendPost(paramMap, tranxSN, "SMZF001");
		if (xmlMessage != null) {
			Map<String, Object> retMap = MinsUtil.parseRet(xmlMessage);
			String merchantCode = xmlMessage.substring(xmlMessage.indexOf("<merchantCode>") + 14, xmlMessage.indexOf("</merchantCode>"));			
			retMap.put("merchantCode", merchantCode);
			return retMap;
		}
		return null;
	}
	
	/**
	 * 民生银行被扫支付
	 * @param tranxSN	订单号
	 * @param merchantCode	商户编码
	 * @param totalAmount	订单金额
	 * @param subject	主题
	 * @return
	 */
	public static Map<String, Object> codePay(String tranxSN, String merchantCode, double totalAmount, String subject){
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("merchantCode", merchantCode);
		paramMap.put("totalAmount", totalAmount);
		paramMap.put("subject", subject);
		String xmlMessage = MinsUtil.sendPost(paramMap, tranxSN, "SMZF002");
		if (xmlMessage != null) {
			Map<String, Object> retMap = MinsUtil.parseRet(xmlMessage);
			if("200002".equals(retMap.get("respCode"))){
				String codeUrl = xmlMessage.substring(xmlMessage.indexOf("<qrCode>") + 8, xmlMessage.indexOf("</qrCode>"));
				retMap.put("respCode", "00");
				retMap.put("respDesc", "支付中");
				retMap.put("codeUrl", codeUrl);
			}
			return retMap;
		}
		return null;
	}
	
	/**
	 * 民生银行主扫支付
	 * @param tranxSN	订单号
	 * @param merchantCode	商户编码
	 * @param authCode		授权码
	 * @param totalAmount	订单金额
	 * @param subject	订单主题
	 * @return
	 */
	public static Map<String, Object> authPay(String tranxSN, String merchantCode, String authCode,  double totalAmount, String subject){
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("merchantCode", merchantCode);
		paramMap.put("scene", 1);
		paramMap.put("authCode", authCode);
		paramMap.put("totalAmount", totalAmount);
		paramMap.put("subject", subject);
		String xmlMessage = MinsUtil.sendPost(paramMap, tranxSN, "SMZF003");
		if (xmlMessage != null) {
			Map<String, Object> retMap = MinsUtil.parseRet(xmlMessage);
			if("200002".equals(retMap.get("respCode"))){
				retMap.put("respCode", "01");
				retMap.put("respDesc", "支付中");
			}
			return retMap;
		}
		return null;
	}

	/**
	 * 民生银行 退款
	 * @param tranxSN	订单号
	 * @param oriReqMsgId 原订单号
	 * @param refundAmount 退款金额
	 * @return
	 */
	public static Map<String, Object> refundApply(String tranxSN, String oriReqMsgId, double refundAmount){
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("oriReqMsgId", oriReqMsgId);
		paramMap.put("refundAmount", refundAmount);
		String xmlMessage = MinsUtil.sendPost(paramMap, tranxSN, "SMZF004");
		if (xmlMessage != null) {
			return MinsUtil.parseRet(xmlMessage);
		}
		return null;
	}
	
	/**
	 * 民生银行撤销交易
	 * @param tranxSN	订单号
	 * @param oriReqMsgId  原订单号
	 * @return
	 */
	public static Map<String, Object> transRevoke(String tranxSN, String oriReqMsgId){
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("oriReqMsgId", oriReqMsgId);
		String xmlMessage = MinsUtil.sendPost(paramMap, tranxSN, "SMZF005");
		if (xmlMessage != null) {
			return MinsUtil.parseRet(xmlMessage);
		}
		return null;
	}
	
	/**
	 * 民生银行订单查询
	 * @param tranxSN	订单号
	 * @param oriReqMsgId	原订单号
	 * @return
	 */
	public static Map<String, Object> transQuery(String tranxSN, String oriReqMsgId){
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("oriReqMsgId", oriReqMsgId);
		String xmlMessage = MinsUtil.sendPost(paramMap, tranxSN, "SMZF006");
		if (xmlMessage != null) {
			Map<String, Object> retMap = MinsUtil.parseRet(xmlMessage);
			retMap.put("oriRespType", xmlMessage.substring(xmlMessage.indexOf("<oriRespType>") + 13, xmlMessage.indexOf("</oriRespType>")));
			retMap.put("oriRespCode", xmlMessage.substring(xmlMessage.indexOf("<oriRespCode>") + 13, xmlMessage.indexOf("</oriRespCode>")));
			retMap.put("oriRespMsg", xmlMessage.substring(xmlMessage.indexOf("<oriRespMsg>") + 12, xmlMessage.indexOf("</oriRespMsg>")));
			retMap.put("totalAmount", xmlMessage.substring(xmlMessage.indexOf("<totalAmount>") + 13, xmlMessage.indexOf("</totalAmount>")));
			retMap.put("isClearOrCancel", xmlMessage.substring(xmlMessage.indexOf("<isClearOrCancel>") + 17, xmlMessage.indexOf("</isClearOrCancel>")));
			retMap.put("buyerAccount",xmlMessage.substring(xmlMessage.indexOf("<buyerAccount>") + 14, xmlMessage.indexOf("</buyerAccount>")));
			retMap.put("buyerPayAmount",xmlMessage.substring(xmlMessage.indexOf("<buyerPayAmount>") + 16, xmlMessage.indexOf("</buyerPayAmount>")));
			return retMap;
		}
		return null;
	}
	
	public static Map<String, Object> getSettFile(String settleDate, String filePath){
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("settleDate", settleDate);
		paramMap.put("fileType", 1);
		String xmlMessage = MinsUtil.sendPost(paramMap, DateUtil.formatNow("yyyyMMddHHmmss"), "SMZF020");
		if (xmlMessage != null) {
			Map<String, Object> retMap = MinsUtil.parseRet(xmlMessage);
			if("00".equals(retMap.get("respCode"))){
				String content = xmlMessage.substring(xmlMessage.indexOf("<content>") + 9, xmlMessage.indexOf("</content>"));
				File file =new File(filePath);
				try (FileOutputStream fos = new FileOutputStream(file)){
					if (!file.exists()) file.createNewFile();
					byte[] contentInBytes = content.getBytes();
					fos.write(contentInBytes);
					fos.flush();
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return retMap;
		}
		return null;
	}
	
	public static void main(String[] args) {
		//2016090704433182
//		System.out.println(merchantApply(2, "201909071747", "P000813732228909", "青岛澳维酒业有限公司", "青岛澳维", "青岛市市北区山东路117号3号楼1单元403户", "13732228909", "208"));
//		System.out.println(authPay("201609072232", "2016090704433182", "130205484174979375", 1, "测试啊"));
//		System.out.println(codePay("201609072237", "2016090704433182", 2, "测试二维码"));
//		System.out.println(refundApply("201609062227", "201609072237", 1));
//		System.out.println(getSettFile("20160913", "d:\\0913.TXT"));
		System.out.println(transQuery("12345grsdf", "OR1017151934014139461"));
		//短款  OR 
		//长款  8笔
	}
}
